﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using ClassLibrary1;
using Domain.Entities;

namespace Webservice.Controllers
{
    public class HealthCareEntityTypesController : ApiController
    {
        private MyHealthDbContext db = new MyHealthDbContext();

        // GET: api/HealthCareEntityTypes
        public IQueryable<HealthCareEntityType> GetHealthCareEntityTypes()
        {
            return db.HealthCareEntityTypes;
        }

        // GET: api/HealthCareEntityTypes/5
        [ResponseType(typeof(HealthCareEntityType))]
        public IHttpActionResult GetHealthCareEntityType(int id)
        {
            HealthCareEntityType healthCareEntityType = db.HealthCareEntityTypes.Find(id);
            if (healthCareEntityType == null)
            {
                return NotFound();
            }

            return Ok(healthCareEntityType);
        }

        // PUT: api/HealthCareEntityTypes/5
        [ResponseType(typeof(void))]
        public IHttpActionResult PutHealthCareEntityType(int id, HealthCareEntityType healthCareEntityType)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != healthCareEntityType.Id)
            {
                return BadRequest();
            }

            db.Entry(healthCareEntityType).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!HealthCareEntityTypeExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/HealthCareEntityTypes
        [ResponseType(typeof(HealthCareEntityType))]
        public IHttpActionResult PostHealthCareEntityType(HealthCareEntityType healthCareEntityType)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.HealthCareEntityTypes.Add(healthCareEntityType);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = healthCareEntityType.Id }, healthCareEntityType);
        }

        // DELETE: api/HealthCareEntityTypes/5
        [ResponseType(typeof(HealthCareEntityType))]
        public IHttpActionResult DeleteHealthCareEntityType(int id)
        {
            HealthCareEntityType healthCareEntityType = db.HealthCareEntityTypes.Find(id);
            if (healthCareEntityType == null)
            {
                return NotFound();
            }

            db.HealthCareEntityTypes.Remove(healthCareEntityType);
            db.SaveChanges();

            return Ok(healthCareEntityType);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool HealthCareEntityTypeExists(int id)
        {
            return db.HealthCareEntityTypes.Count(e => e.Id == id) > 0;
        }
    }
}